package coml.allen.api.dlx;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sesshomaru
 * @date 2020/11/14 16:23
 *
 * 这里测试使用的消息过期 然后自动到达 死信队列
 */
public class Consumer {

    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();

        connectionFactory.setHost("192.168.1.102");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        // 开启消息发送确认
        channel.confirmSelect();

        String exchange = "test_dlx_ex";
        String routingKey = "dlx.#";
        String queueName = "test-dlx-queue";

        // 死信队列和交换机和绑定key
        String DlxExchange = "dlx_ex";
        String DlxRoutingKey = "#";
        String DlxQueueName = "dlx-queue";
        channel.exchangeDeclare(DlxExchange, "topic", true);
        channel.queueDeclare(DlxQueueName, true, false, false, null);
        channel.queueBind(DlxQueueName,DlxExchange, DlxRoutingKey);


        // 设置TTL 队列
        Map<String, Object> arguments = new HashMap<>();
        /**
         * 其他可以设置的属性，死信队列什么的啊
         * Message TTL  | Auto expire  | Max length  | Max length bytes  | Overflow behaviour
         * Dead letter exchange  | Dead letter routing key  | Single active consumer  | Maximum priority
         * Lazy mode  Master locator
         */
        // 单位毫秒
        arguments.put("x-message-ttl", 20000);
        // 死信配置
        arguments.put("x-dead-letter-exchange",DlxExchange);
        arguments.put("x-dead-letter-routing-key",DlxRoutingKey);
        channel.exchangeDeclare(exchange, "topic", true);
        channel.queueDeclare(queueName, true, false, false, arguments);
        channel.queueBind(queueName, exchange, routingKey);

        //channel.basicConsume(queueName, false, new MyConsumer(channel));
    }
}
